/**
 * Created by 1 on 2017-06-19.
 */
import userApi from '../../api/userApi'
import * as types from '../mutation-types'

// initial state
// shape: [{ id, quantity }]
const state = {
    loginStatus: '',
    loginError: [],
    userInfo: [],
    userSessionData: {
        loading: -1,
        code: '',
        msg: '',
        data: ''
    }
};

// getters
const getters = {
    loginStatus: state => state.loginStatus,
    loginError: state => state.loginError,
    loginUser: state => state.userInfo,
    userSessionData: state => state.userSessionData
};

// actions
const actions = {
    login ({ commit }, loginData) {
        console.debug('login data ', loginData);
        commit(types.By_User_Login_Req);
        userApi.login(
            loginData,
            (res) => {
                commit(types.By_User_Login_Suc, res);
            },
            (res) => {
                commit(types.By_User_Login_Fail, res);
            }
        )
    },
    getUserSessionData ({ commit }) {
        console.debug('用户登录成功后获取用户会话数据');
        commit(types.By_User_Session_Data_Req);
        let sessionData = cache.getBigDataValue(types.By_User_Session_Data_Req);
        if (sessionData && sessionData !== '') {
            console.debug('[cache] getUserSessionData 使用缓存');
            sessionData = tools.base64Utils.decode(sessionData);
            commit(types.By_User_Session_Data_Suc, JSON.parse(sessionData));
            return;
        }
        userApi.getUserData((res) => {
            commit(types.By_User_Session_Data_Suc, res);
        }, (res) => {
            commit(types.By_User_Session_Data_Fail, res);
        });
    },
    logout ({ commit }, userInfo) {
        console.debug('用户登出', userInfo);
        commit(types.By_User_Logout_Req);
        userApi.logout(
            userInfo,
            (res) => {
                commit(types.By_User_Logout_Done, res);
            },
            (res) => {
                commit(types.By_User_Logout_Done, res);
            }
        )
    }
};

// mutations
const mutations = {
    [types.By_User_Logout_Done] (state) {
        console.debug('By_User_Logout_Done');
        state.loginStatus = types.By_User_Logout_Done;
        state.userInfo = [];
        cache.clear();
    },
    [types.By_User_Logout_Req] (state) {
        console.debug('By_User_Logout_Req');
        state.loginStatus = types.By_User_Logout_Req;
        state.userInfo = [];
    },
    [types.By_User_Login_Req] (state) {
        console.debug('By_User_Login_Req');
        state.userInfo = [];
        state.loginStatus = 'by_user_login_req';
        state.loginError = [];
    },
    [types.By_User_Login_Suc] (state, data) {
        console.debug('By_User_Login_Suc');
        if (data.code === 0) {
            state.userInfo = data.data;
            state.loginStatus = types.By_User_Login_Suc;
        } else {
            state.userInfo = [];
            state.loginStatus = types.By_User_Login_Fail;
        }
    },

    [types.By_User_Login_Fail] (state, data) {
        // rollback to the cart saved before sending the request
        state.loginStatus = types.By_User_Login_Fail; // 'by_user_login_fail';
        state.loginError = { ...state.loginError, msg: data.msg, code: data.code, data: data.data };
        console.debug('By_User_Login_Fail', state.loginError);
    },
    [types.By_User_Session_Data_Req] (state) {
        console.debug('By_User_Session_Data_Req');
        // state.userSessionData.code = '';
        // state.userSessionData.msg = '';
        // state.userSessionData.data = '';
        state.userSessionData.loading = 2;
    },
    [types.By_User_Session_Data_Fail] (state, data) {
        // rollback to the cart saved before sending the request
        state.userSessionData.loading = -1;
        state.userSessionData = { ...state.userSessionData, msg: data.msg, code: data.code, data: data.data };
        console.debug('By_User_Session_Data_Fail', state.loginError);
    },
    [types.By_User_Session_Data_Suc] (state, data) {
        // rollback to the cart saved before sending the request
        // console.debug('数据转化', data, JSON.stringify(data), tools.base64Utils.encode(JSON.stringify(data)));
        cache.setBigDataValue(types.By_User_Session_Data_Req, tools.base64Utils.encode(JSON.stringify(data)), 3600);
        state.userSessionData.loading = 1;
        state.userSessionData = { ...state.userSessionData, msg: data.msg, code: data.code, data: data.data };
        console.debug('By_User_Session_Data_Suc', data, state.userSessionData);
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}